﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows.Forms;

using RexToy.Validation;

namespace RexToy.UI.WinForm.ViewModel
{
    public abstract class ViewModelBase : INotifyPropertyChanged
    {
        public string Key { get; set; }

        public virtual IValidateResult Validate()
        {
            return ValidateResult.NoError;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChange(string name)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(name);
                handler(this, e);
            }
        }

        protected virtual void OnPropertyChange<T>(Expression<Func<T, object>> expr)
        {
            string name = PropertyExtract.ExtractName(expr);
            OnPropertyChange(name);
        }

        public event Action Close;
        protected virtual void OnClose()
        {
            Action act = this.Close;
            if (act != null)
                act();
        }

        internal void OnClosing()
        {
            Closing();
        }

        protected virtual void Closing()
        {
        }
    }
}
